In [1]:
import numpy as np
In [2]:
import sys
# local api/python3 path - adjust path for your system
japipath = 'C:\\j64\\j64-807\\addons\\api\\python3'
if japipath not in sys.path:
sys.path.append(japipath)
sys.path
Out[2]:
In [3]:
import jbase as j
print(j.__doc__)
In [4]:
# start J - only one instance currently allowed
try:
j.init()
except:
print('j running')
In [5]:
j.dor("i. 2 3 4") # run sentence and print output result
In [6]:
rc = j.do(('+a.')) # run and return error code
print(rc)
j.getr() # get last output result
Out[6]:
In [7]:
j.do('abc=: i.2 3') # define abc
q= j.get('abc') # get q as numpy array from J array
print (q)
In [8]:
j.set('ghi',23+q) # set J array from numpy array
j.dor('ghi') # print array (note typo in addon (j.__doc___)
In [9]:
j.do("cows =. 'don''t have a cow man'")
j.get('cows')
Out[9]:
In [10]:
ido = "I do what I do because I am what I am!"
j.set("ido", ido)
j.dor("ido")
In [11]:
# decomment to run REPL
# j.j()
In [12]:
# boolean numpy array
p = np.array([True, False, True, True]).reshape(2,2)
In [13]:
p
Out[13]:
In [14]:
j.set("p", p)
In [15]:
j.dor("p")
As you can see a round trip of numpy booleans generates digital noise.
The only numpy datatypes J natively supports on Win64 systems are:
np.int64
np.float64
simple character strings - passed as bytes
To use other types it will be necessary to encode and decode them with Python and J helper functions.
The limited datatype support is not as limiting as you might expect. The default NumPy
array is
np.float64
on 64 bit systems and the majority of NumPy
based packages manipulate floating point
and integer arrays.
The following NumPy
examples are from the SciPy.org's
NumPy quick start tutorial. For each NumPy
statement, I have provided a J equivalent
In [16]:
# numpy
a = np.arange(15).reshape(3, 5)
print(a)
In [17]:
# J
j.do("a =. 3 5 $ i. 15")
j.dor("a")
In [18]:
# numpy
a = np.array([2,3,4])
print(a)
In [19]:
# J
j.do("a =. 2 3 4")
j.dor("a")
In [20]:
# numpy
b = np.array([(1.5,2,3), (4,5,6)])
print(b)
In [21]:
# J
j.do("b =. 1.5 2 3 ,: 4 5 6")
j.dor("b")
In [22]:
# numpy
c = np.array( [ [1,2], [3,4] ], dtype=complex )
print(c)
In [23]:
# J
j.do("c =. 0 j.~ 1 2 ,: 3 4")
j.dor("c") # does not show as complex
j.dor("datatype c") # c is complex
In [24]:
# numpy - make complex numbers with nonzero real and imaginary parts
c + (0+4.7j)
Out[24]:
In [25]:
# J - also for J
j.dor("c + 0j4.7")
In [26]:
# numpy
np.zeros( (3,4) )
Out[26]:
In [27]:
# J
j.dor("3 4 $ 0")
In [28]:
# numpy - allocates array with whatever is in memory
np.empty( (2,3) )
Out[28]:
In [29]:
# J - uses fill - safer but slower than numpy's trust memory method
j.dor("2 3 $ 0.0001")
In [30]:
# numpy
a = np.array( [20,30,40,50] )
b = np.arange( 4 )
c = a - b
print(c)
In [31]:
# J
j.do("a =. 20 30 40 50")
j.do("b =. i. 4")
j.do("c =. a - b")
j.dor("c")
In [32]:
# numpy - uses previously defined (b)
b ** 2
Out[32]:
In [33]:
# J
j.dor("b ^ 2")
In [34]:
# numpy - uses previously defined (a)
10 * np.sin(a)
Out[34]:
In [35]:
# J
j.dor("10 * 1 o. a")
In [36]:
# numpy - booleans are True and False
a < 35
Out[36]:
In [37]:
# J - booleans are 1 and 0
j.dor("a < 35")
In [38]:
# numpy
a = np.array( [[1,1], [0,1]] )
b = np.array( [[2,0], [3,4]] )
# elementwise product
a * b
Out[38]:
In [39]:
# J
j.do("a =. 1 1 ,: 0 1")
j.do("b =. 2 0 ,: 3 4")
j.dor("a * b")
In [40]:
# numpy - matrix product
np.dot(a, b)
Out[40]:
In [41]:
# J - matrix product
j.dor("a +/ . * b")
In [42]:
# numpy - uniform pseudo random - seeds are different in Python and J processes - results will differ
a = np.random.random( (2,3) )
print(a)
In [43]:
# J - uniform pseudo random
j.dor("?. 2 3 $ 0")
In [44]:
# numpy - sum all array elements - implicit ravel
a = np.arange(100).reshape(20,5)
a.sum()
Out[44]:
In [45]:
# j - sum all array elements - explicit ravel
j.dor("+/ , 20 5 $ i.100")
In [46]:
# numpy
b = np.arange(12).reshape(3,4)
print(b)
# sum of each column
print(b.sum(axis=0))
# min of each row
print(b.min(axis=1))
# cumulative sum along each row
print(b.cumsum(axis=1))
# transpose
print(b.T)
In [47]:
# J
j.do("b =. 3 4 $ i. 12")
j.dor("b")
# sum of each column
j.dor("+/ b")
# min of each row
j.dor('<./"1 b')
# cumulative sum along each row
j.dor('+/\\"0 1 b') # must escape \ character to pass +/\"0 1 properly to J
# transpose
j.dor("|: b")
In [48]:
# numpy
a = np.arange(10) ** 3
print(a[2])
print(a[2:5])
print(a[ : :-1]) # reversal
In [49]:
# J
j.do("a =. (i. 10) ^ 3")
j.dor("2 { a")
j.dor("(2 + i. 3) { a")
j.dor("|. a")
In [50]:
from numpy import pi
x = np.linspace( 0, 2*pi, 100, np.float64) # useful to evaluate function at lots of points
f = np.sin(x)
f
Out[50]:
In [51]:
j.set("f", f)
In [52]:
j.get("f")
Out[52]:
In [53]:
r = np.random.random((2000,3000))
r = np.asarray(r, dtype=np.float64)
In [54]:
r
Out[54]:
In [55]:
j.set("r", r)
In [56]:
j.get("r")
Out[56]:
In [57]:
r.shape
Out[57]:
In [58]:
j.get("r").shape
Out[58]:
In [59]:
j.dor("r=. ,r")
j.get("r").shape
Out[59]:
In [60]:
r.sum()
Out[60]:
In [61]:
b = np.ones((5,300,4), dtype=np.int64)
j.set("b", b)
b2 = j.get("b")
print(b.sum())
print(b2.sum())